<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${mapper_pkg}.${bean_type}Mapper">
	
	<resultMap type="${bean_type}" id="${bean_type}ResultMap">
		<#list bean_property as p> <#if p.columnFields?lower_case="id"><id property="id" column="ID"/>
		<#else>
		 <result property="${p.fields}" column="${p.columnFields?upper_case}"/>
		</#if>
		</#list>
	</resultMap>
	
	<insert id="insert" parameterType="${bean_type}" useGeneratedKeys="true" keyProperty="id">
		<![CDATA[
			INSERT INTO ${table_name}(<#assign i = 0><#list bean_property as p><#if p.columnFields?lower_case != "id">${p.columnFields?upper_case}<#if i != (bean_property?size - 1)>, </#if></#if><#assign i = i + 1></#list>) 
			VALUES(<#assign i = 0><#list bean_property as p><#if p.columnFields?lower_case != "id">#${'{'}${p.fields},jdbcType=${p.jdbcType}}<#if i != (bean_property?size - 1)>, </#if></#if><#assign i = i + 1></#list> )
		]]>
	</insert>
	
	<delete id="delete" parameterType="${bean_type}">
		 delete from ${table_name}
		 <trim prefix="where" prefixOverrides="AND |OR">  
			<#list bean_property as p> 
			<if test="${p.fields} != null and ${p.fields} != ''">
				AND ${ p.columnFields?upper_case} = #${'{'}${p.fields}}
			</if>
			</#list>
		 </trim> 
	</delete>
	
	<delete id="deleteById" parameterType="Long">
		 delete from ${table_name} where id=${r"#{id}"}  
	</delete>
	
	<delete id="deleteByIds" parameterType="list">
		 delete from ${table_name} where id in 
		 <foreach collection="list" item="ids"  open="(" separator="," close=")">   
           #${'{'}ids}
        </foreach>  
	</delete>
		
	
	<update id="updateById" parameterType="${bean_type}">
			UPDATE ${table_name} 
			 <trim prefix="SET" suffixOverrides=",">
				<#list bean_property as p> 
				<if test="${p.fields} != null and ${p.fields} != '' ">
					${p.columnFields?upper_case} = #${'{'}${p.fields}},
				</if>
				</#list>
			</trim>
			WHERE ID= #${'{'}id}
	</update>
	
	<sql id="queryFields">
		<#assign i = 0><#list bean_property as p>${p.columnFields?upper_case}<#if i != (bean_property?size - 1)>, </#if><#assign i = i + 1></#list>
	</sql>
	
	<sql id="queryCondition">
		<trim prefix="where" prefixOverrides="AND |OR">  
			<#list bean_property as p> 
			<if test="${p.fields} != null and ${p.fields} != '' ">
				<![CDATA[ AND ${p.columnFields?upper_case} = #${'{'}${p.fields}} ]]>
			</if>
			</#list>
		 </trim> 
	</sql>
	
	<sql id="queryLikeCondition">
		<trim prefix="where" prefixOverrides="AND |OR">  
			<#list bean_property as p> 
			<if test="${p.fields} != null and ${p.fields} != '' ">
				<![CDATA[ AND ${p.columnFields?upper_case} like CONCAT(CONCAT('%', #${'{'}${p.fields}}),'%')]]>
			</if>
			</#list>
		 </trim> 
	</sql>
	
	<select id="count" resultType="long">
		select count(*) from ${table_name}
	</select>
	
	<select id="countSearch" parameterType="${page_query}" resultType="long">
		select count(*) from ${table_name}  
		<include refid="queryCondition"/>
	</select>
	
	<select id="queryForPages" parameterType="${page_query}" resultMap="${bean_type}ResultMap">
		select * from ${table_name}  
		<include refid="queryCondition"/>
		limit  #${'{'}start},#${'{'}limit} 
	</select>
	
	<select id="queryById" resultMap="${bean_type}ResultMap" parameterType="Long">
		 SELECT <include refid="queryFields"/>  FROM ${table_name}
		 where id=${r"#{id}"}  
	</select>
	
	<select id="query" resultMap="${bean_type}ResultMap" parameterType="${bean_type}">
		 SELECT <include refid="queryFields"/> FROM ${table_name}
		 <include refid="queryCondition"/>
	</select>
    
    <select id="queryForPage" resultMap="${bean_type}ResultMap" parameterType="hashmap">
		SELECT <include refid="queryFields"/> FROM ${table_name}
		 <include refid="queryCondition"/>
		 limit #${'{'}start},#${'{'}limit} 
	</select>
    
</mapper>